/* improvement over the built-in ebay-search command  for Ubiquity*/
CmdUtils.CreateCommand({
  name: "ebay-search",
  homepage: "http://mytechieself.blogspot.com",
  author: {name: "Mskadu", email: "mskadu@gmail.com"},
  license: "MPL",
  description: "Searches your local Ebay website for auctions matching the given words",
  icon: "http://search.ebay.com/favicon.ico",
  takes: {"search": noun_arb_text},
  _getFirstLanguage: function() {
    /* Queries the Firefox config settings and picks up the first language
     * from the list 
     */
    var langs = Application.prefs.get("intl.accept_languages");
    var first_lang = (langs.value.split(","))[0];
    //displayMessage( first_lang );
    return first_lang;
  },
  _isFrench: function( language_code ) {
    /* check whether given language code is one of the French ones */
    if( (language_code == "fr") || (language_code.indexOf("fr-") != -1) )
      return true;
    else
      return false;
  },
  _isDutch: function( language_code ) {
    /* check whether given language code is one of the Dutch ones */
    if( (language_code == "nl") || language_code == "nl-be" )
      return true;
    else
      return false;
  },
  _generateSearchURL: function() {
      /* ISO Codes vs. Ebay URLs */
      var isocodes_ebay = {
        "AG": "http://listado.mercadolibre.com.ar/",
        "AU": "http://shop.ebay.com.au/?_nkw=",
        "AT": "http://search.ebay.at/search/search.dll?satitle=",
        /* Exception for Belgium as the site comes in French and Dutch versions */
        "BE_NL": "http://search.benl.ebay.be/search/search.dll?satitle=",
        "BE_FR": "http://search.befr.ebay.be/search/search.dll?satitle=",
        "BR": "http://lista.mercadolivre.com.br/",
        "CA": "http://search.ebay.ca/search/search.dll?satitle=",
        /* "CN": "http://search.eachnet.com/Search", */ /* Unsupported as the site requires POST */
        "FR": "http://search.ebay.fr/search/search.dll?satitle=",
        "DE": "http://search.ebay.de/search/search.dll?satitle=",
        "HK": "http://search.ebay.com.hk/search/search.dll?satitle=",
        "IN": "http://search.ebay.in/search/search.dll?satitle=",
        "IE": "http://search.ebay.ie/search/search.dll?satitle=",
        "IT": "http://search.ebay.it/search/search.dll?satitle=",
        "KP": "http://search.auction.co.kr/search/search.aspx?keyword=",
        "MY": "http://search.ebay.com.my/search/search.dll?satitle=",
        "MX": "http://listado.mercadolibre.com.mx/",
        "NL": "http://search.ebay.nl/search/search.dll?satitle=",
        "NZ": "http://search.ebay.com/search/search.dll?salic=149&cgiurl=http%3A//cgi.ebay.com/ws/&sokeywordredirect=1&from=R8&satitle=",
        "PH": "http://search.ebay.ph/search/search.dll?satitle=",
        "PL": "http://search.ebay.pl/search/search.dll?satitle=",
        "SG": "http://search.ebay.com.sg/search/search.dll?satitle=",
        "ES": "http://search-desc.ebay.es/search/search.dll?satitle=",
        "SE": "http://www.tradera.com/search/result.aspx?search=",
        "CH": "http://search.ebay.ch/search/search.dll?satitle=",
        "TW": "http://search.ruten.com.tw/search/s000.php?k=",
        "TH": "http://shopping.sanook.com/search_result.php?q=",
        "GB": "http://shop.ebay.co.uk/?_nkw=",
        "US": "http://search.ebay.com/search/search.dll?satitle=",
        "VN": "http://buy.ebay.vn/buying/vn/search/?satitle="
      };
                        
    var cc = CmdUtils.getGeoLocation().country_code;

    /* Decide whether Belgian users have French or Dutch set as their first language */
    if( cc == "BE" ) { 
      var which_belgian_language = this._getFirstLanguage();
      if( this._isFrench( which_belgian_language ) ) {
          cc = "BE_FR";
      }
      else if( this._isDutch( which_belgian_language ) ) {
          cc = "BE_NL";
      }
      else {
          cc = "BE_FR"; //default fall-back
      }

    }
    

    var searchURL = isocodes_ebay[cc];
    if( !searchURL )
    {
      /* Default to US if the users country does not feature in the 
       * list of Ebays country-specific websites 
       */
      searchURL = isocodes_ebay["US"];
    }

    return searchURL;
  },  
  preview: function( pblock, search ) {
          
          if( search.text )
            pblock.innerHTML = "Searches EBay for " + search.text;
          else
            pblock.innerHTML = "Searches EBay for the given words";
        },
  execute: function( search ) {
          if( search.text ) {
            var baseURL = this._generateSearchURL();
            var searchURL = baseURL + encodeURIComponent( search.text ) ;
            Utils.openUrlInBrowser( searchURL );
          }
          else {
            displayMessage( "Nothing to search for" );
          }
        }
});


